package com.sf.rbac.web.controller;


import com.github.pagehelper.PageInfo;
import com.sf.rbac.anno.RequiredPermission;
import com.sf.rbac.domain.Permission;
import com.sf.rbac.qo.QueryObject;
import com.sf.rbac.service.IPermissionService;
import com.sf.rbac.util.JsonResult;
import com.sf.rbac.util.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @GetMapping("/list")
    @RequiredPermission(name = "权限列表", expression = "permission:list")
    public JsonResult list(QueryObject queryObject) {
        PageInfo<Permission> pageInfo = permissionService.query(queryObject);
        PageResult result = new PageResult();
        BeanUtils.copyProperties(pageInfo, result);
        return JsonResult.success(result);
    }

    @PostMapping("/load")
    @RequiredPermission(name = "权限加载", expression = "permission:load")
    public JsonResult load() {
        permissionService.load();
        return JsonResult.success();
    }

    @GetMapping("/queryPermission/{id}")
    public JsonResult queryPermission(@PathVariable Long id){
        List<Permission> perms= permissionService.queryPermission(id);
        return JsonResult.success(perms);
    }
    @GetMapping("/listAll")
    public JsonResult listAll(){
        return JsonResult.success(permissionService.listAll());
    }
}
